package com.example.graphicslib.animations;

import android.util.Log;

import com.example.graphicslib.utils.Function;

/**
 * Created by Tatev on 11/11/13.
 */
public class BaseMovementAnimation implements Movable{

    public BaseMovementAnimation(float startX, float startY, long duration) {
        this.currentX = startX;
        this.currentY = startY;
        this.duration = duration;
    }

    @Override
    public void setUpTranslation(float toX, float toY) {
        destinationX = toX;
        destinationY = toY;

        calculate();
        setAcceleration(0);
    }

    @Override
    public void scheduleTranslation(long delay, long duration) {

    }

    @Override
    public long getDuration() {
        return duration;
    }

    @Override
    public float getCurrentX() {
        return currentX;
    }

    @Override
    public float getCurrentY() {
        return currentY;
    }

    public void setVelocity(float velocity){
        velocityX = (float)(velocity * distanceX / (Math.sqrt(distanceX * distanceX + distanceY * distanceY)));
        velocityY = (float)(velocity * distanceY / (Math.sqrt(distanceX * distanceX + distanceY * distanceY)));
    }

    public void setVelocity(Function f){
        velocityFunction = f;
    }

    public void setAcceleration(float acceleration){
        accelerationX = (float)(acceleration * distanceX / (Math.sqrt(distanceX * distanceX + distanceY * distanceY)));
        accelerationY = (float)(acceleration * distanceY / (Math.sqrt(distanceX * distanceX + distanceY * distanceY)));
    }

    public void setAcceleration(Function f){
        accelerationFunction = f;
    }

    @Override
    public void onTickResponse(long elapsedTime) {

        if(elapsedTime < duration){
            if(velocityFunction != null){
                elapsedTime /= 3600;
                setVelocity((float)velocityFunction.apply(elapsedTime));
            }

            if(accelerationFunction != null){
                elapsedTime /= 12960000;
                setAcceleration((float)accelerationFunction.apply(elapsedTime));
            }

            Log.d("%%%%%%%%%%%%%", "onTickResponse");
            //if(currentX != destinationX && currentY != destinationY){
                currentX += velocityX * elapsedTime + accelerationX * elapsedTime * elapsedTime / 2;
                currentY += velocityY * elapsedTime + accelerationY * elapsedTime * elapsedTime / 2;
                Log.d("%%%%%%%%%%%%%", "currentX: " + currentX + ", currentY: " + currentY);
            //}
        }
    }

    @Override
    public void onFinishResponse() {

    }


    private void calculate() {
        distanceX = destinationX - currentX;
        distanceY = destinationY - currentY;

        velocityX = distanceX / duration;
        velocityY = distanceY / duration;
    }

    private float currentX;
    private float currentY;
    private float destinationX;
    private float destinationY;
    private float deltaX;
    private float deltaY;
    private float distanceX;
    private float distanceY;
    private float velocityX;
    private float velocityY;
    private float accelerationX;
    private float accelerationY;
    private long duration;
    private Function velocityFunction;
    private Function accelerationFunction;
}
